Is there any danger in calling free() or delete instead of delete[]? [closed]

Posted by Matt Joiner on Stack Overflow See other posts from Stack Overflow or by Matt Joiner
Published on 2009-10-23T08:14:54Z Indexed on 2010/03/25 9:43 UTC
Read the original article Hit count: 486

Filed under:
|
|
|
|

Possible Duplicate:
( POD )freeing memory : is delete[] equal to delete ?

Does delete deallocate the elements beyond the first in an array?

char *s = new char[n];
delete s;

Does it matter in the above case seeing as all the elements of s are allocated contiguously, and it shouldn't be possible to delete only a portion of the array?

For more complex types, would delete call the destructor of objects beyond the first one?

Object *p = new Object[n];
delete p;

How can delete[] deduce the number of Objects beyond the first, wouldn't this mean it must know the size of the allocated memory region? What if the memory region was allocated with some overhang for performance reasons? For example one could assume that not all allocators would provide a granularity of a single byte. Then any particular allocation could exceed the required size for each element by a whole element or more.

For primitive types, such as char, int, is there any difference between:

int *p = new int[n];
delete p;
delete[] p;
free p;

Except for the routes taken by the respective calls through the delete->free deallocation machinery?

© Stack Overflow or respective owner

Related posts about c++

Related posts about new